<?php
/**
 * Created by PhpStorm.
 * User: shkj
 * Date: 2020/4/19
 * Time: 15:46
 */

namespace app\api\model;


use think\Model;
use think\Db;
class Store extends Model
{

    protected $autoWriteTimestamp = true;

    protected $createTime='create_time';
    protected $dateFormat='Y年m月d日';
    //开启自动设置
    protected $auto = [];
    protected $insert = ['create_tiame'];

    public function img(){
        return $this->belongsTo('Image','img_id','id');
    }

    public function user(){
        return $this->belongsTo('User','uid','id');
    }

    public function select(){
        $res = self::all([],'img,user');

        foreach ($res as $key => $value){

            $res[$key] = $value->hidden(['user.id','user.password','user.token','user.photo', 'user.create_time',
                'user.identity','user.activation','user.ip']);

        }

        $res = $res->toArray();
        foreach ($res as $key => $value) {
            if (is_array($value['img']) && is_array($value['user'])) {
                foreach ($value['img'] as $ik => $iv) {
                    foreach ($value['user'] as $k => $v) {

                        $value[$k] = $v;
                        $value[$ik] = $iv;

                        $res[$key] = $value;
                    }
                    unset($res[$key]['user'],$res[$key]['img']);
                }

            }

//            if (is_array($value['user'])) {
//                foreach ($value['user'] as $ik => $iv) {
//                    $value[$ik] = $iv;
//
//                    $res[$key] = $value;
//
//                    unset($res[$key]['user'],$res[$key]['img']);
//                }
//
//            }

        }
        if($res){
            return_json(200,'查询成功',$res);
        }else{
            return_json(400,'查询失败');
        }
    }

    public function add($data)
    {
        $arr = array();
        $arr['images'] = $data['images'];

        $data['img_id'] = Db::name('image')->insertGetId($arr);//图片id

        $store = self::where('store_name', $data['store_name'])->find();
        if ($store) {
            return_json(400, '该店铺名已存在');
        }
        else {
            $res = self::save($data);
            if ($res) {
                return_json(200, '店铺申请提交成功');
            } else {
                return_json(400, '店铺申请提交失败');
            }

        }
    }
    public function detail($id){
        $res = self::where('id',$id)->with('img,user')->find();
        $res = $res->hidden(['user.id','user.password','user.token','user.photo', 'user.create_time',
            'user.identity','user.activation','user.ip']);
        $res = $res->toArray();
        foreach ($res['img'] as $k=>$v){
            foreach ($res['user'] as $key=>$value){
                $res[$key]=$value;
            }
            $res[$k] = $v;
        }
        unset($res['img'],$res['user']);
        if($res){
            return_json(200,'查询成功',$res);
        }else{
            return_json(400,'查询失败');
        }
    }

    public function del($id){
        $img = self::where('id',$id)->with('img')->find();

        $res = $img->together('img')->delete();
        if($res){
            return_json(200,'删除成功');
        }else{
            return_json(400,'删除失败');
        }
    }

    public function modify($data)
    {
        $img = Image::where('images', $data['images'])->find();
        if ($img) {
            $data['img_id'] = $img['id'];
            $this->perform($data);
        }
        else {
            $img = self::where('id',$data['id'])->with('img')->find();

            if(!empty($img['img']['images'])){
                Image::where('images',$img['img']['images'])->delete();
            }

            $arr = array();
            $arr['images'] = $data['images'];

            $data['img_id'] = Db::name('image')->insertGetId($arr);//图片id
            $this->perform($data);

        }
    }

    public function perform($data){
        $store = self::where('store_name', $data['store_name'])->where('id', '<>', $data['id'])->find();
        if ($store) {
            return_json(400, '该店铺名已经存在');
        } else {
            unset($data['images']);
            $res = self::where('id', $data['id'])->update($data);
            if ($res) {
                return_json(200, '修改成功');
            } else {
                return_json(400, '修改失败');
            }
        }
    }
}